Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check if a number is NaN in ActionParameterHandler #815

Merged

Conversation

borjapazr
Copy link
Contributor

Description

@Param('id') id: number is not validated and accepts NaN as value in case of invalid id.

For example, if the following URL is accessed (http://example.com/users/asdf), instead of throwing an error, the id value is parsed to NaN. InvalidParamError(value, parameterName, parameterType) should be thrown instead.

The problem is due to how the checking of the result of the cast operation is done. NaN is not equal to NaN. Because of this it is necessary to use Number.isNaN().

Example

@JsonController('/users')
class UserController {

  @Get('/:id')
  get (@Param('id') id: number) {
    console.log(id);
  }
}

Result

GET http://example.com/users/1234 -> 1234
GET http://example.com/users/asdf -> NaN

Checklist

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated
  • I have run the project locally and verified that there are no errors

Fixes

fixes #348

@borjapazr
Copy link
Contributor Author

@jotamorais could you review this PR? 😁

@jotamorais jotamorais merged commit 98173dd into typestack:develop Jan 12, 2022
@borjapazr borjapazr deleted the fix/action-parameter-handler branch January 12, 2022 14:03
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

@Param default validation
2 participants